package com.useeinfo.oa.modules.wuHanCommon.base.dao;

import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import com.useeinfo.oa.modules.wuHanCommon.base.entity.WarningGroupInnerUser;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("WarningGroupInnerUserDao")
public class WarningGroupInnerUserDao implements CrudDao<WarningGroupInnerUser> {
    private final static Logger logger = LoggerFactory.getLogger(WarningGroupInnerUserDao.class);

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public Map<String, Object> getSearchCondition(Map<String, String> queryHash) {
        Map<String, Object> conditionHash = new HashMap<>();

        conditionHash.put("deleteStatus = ?{paramIndex} ", false);
        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }
        String warningGroupId = queryHash.get("warningGroupId");
        if (StringUtils.isNotBlank(warningGroupId)) {
            conditionHash.put("warningGroup.id = ?{paramIndex} ", warningGroupId);
        }

        return conditionHash;
    }

    @Override
    public Long totalRecord(Map<String, String> queryHash) {
        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(w) from WarningGroupInnerUser w ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    @Override
    public List<WarningGroupInnerUser> findList(QueryParam queryParam) {
        String sqlInfo = queryParam.joinJPQL("select w from WarningGroupInnerUser w ", "order by w.id desc ");
        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<WarningGroupInnerUser> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, WarningGroupInnerUser.class);
        return queryParam.findPageList(typedQuery);
    }

    @Override
    public WarningGroupInnerUser findModel(Long id) {
        return entityManager.find(WarningGroupInnerUser.class, id);
    }

    @Override
    public Integer add(WarningGroupInnerUser model) {
        model.setCreateDate(new Date());
        entityManager.persist(model);
        logger.info("WarningGroupInnerUserDaoImpl添加WarningGroupInnerUser成功！");
        return 1;
    }

    @Override
    public Integer update(WarningGroupInnerUser model) {
        model.setUpdateDate(new Date());
        entityManager.merge(model);
        return 1;
    }

    @Override
    public Integer delete(Long id) {
        WarningGroupInnerUser warningGroupInnerUser = entityManager.find(WarningGroupInnerUser.class, id);
        entityManager.remove(warningGroupInnerUser);
        return 1;
    }

    @Override
    public Integer deleteLogic(WarningGroupInnerUser model) {
        return null;
    }

    /**
     * 删除报警组的人员
     * @param warning_group_id
     * @return
     */
    public synchronized Integer deleteUserByGroupId(Long warning_group_id) {
        String sql = "DELETE FROM `warning_group_inner_user` WHERE warning_group_id="+warning_group_id;
        Query query = entityManager.createNativeQuery(sql);
        try {
            return query.executeUpdate();
        } catch (Exception ex) {
            return 0;
        }
    }

    /**
     * 获取报警组的人员
     * @param warning_group_id
     * @return
     */
    public List getUserByGroupId(Long warning_group_id) {
        String sql = "SELECT user_id FROM `warning_group_inner_user` WHERE warning_group_id="+warning_group_id;
        return entityManager.createNativeQuery(sql).getResultList();
    }
}
